﻿using System;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FacialClub.Helpers;
using FacialClub.Models.Entities;
using FacialClub.Models.Interfaces;
using FacialClub.Models.Repositories;
using FacialClub.Models.ViewModels;

namespace FacialClub.Areas.Admin.Controllers
{
    /// <summary>
    /// Room Type Controller
    /// </summary>
    [Authorize]
    public class RoomTypeController : Controller
    {
        #region Fields

        private const int DEFAULTPAGESIZE = 10;
        private readonly IRoomTypeRepository _roomTypeRepository;

        #endregion

        #region Constructors

        /// <summary>
        /// Dependency Injection enabled constructors
        /// </summary>
        public RoomTypeController() : this(new RoomTypeRepository())
        {
        }

        /// <summary>
        /// Room Type Controller Constructor
        /// </summary>
        /// <param name="roomTypeRepository"></param>
        public RoomTypeController(IRoomTypeRepository roomTypeRepository)
        {
            _roomTypeRepository = roomTypeRepository;
        }

        #endregion

        #region Actions

        /// <summary>
        /// Display the listing of Room Type
        /// </summary>
        /// <returns></returns>
        public ActionResult Index(string strQuery, int? page)
        {
            IQueryable<RoomType> roomType = !string.IsNullOrWhiteSpace(strQuery)
                                                    ? _roomTypeRepository.FindRoomTypeByName(strQuery).OrderBy(
                                                        d => d.CreatedDate)
                                                    : _roomTypeRepository.FindAllRoomType().OrderBy(
                                                        d => d.CreatedDate);
            var paginatedRoomType =
                roomType.ToPagedList(page.HasValue ? page.Value - 1 : 0, DEFAULTPAGESIZE);
            return View(paginatedRoomType);
        }

        /// <summary>
        /// Details of Room Type
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Details(int? id)
        {
            if (id != null)
            {
                var roomType = _roomTypeRepository.GetRoomTypeById(id.Value);

                if (roomType == null)
                {
                    return new FileNotFoundResult {Message = "No RoomType found for that id"};
                }

                return View(new RoomTypeViewModel(roomType));
            }

            return new FileNotFoundResult {Message = "No RoomType found due to invalid RoomType id"};
        }

        /// <summary>
        /// Create Room Type
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public ActionResult Create()
        {
            var roomType = new RoomType();
            return View(new RoomTypeViewModel(roomType));
        }

        /// <summary>
        /// Create Room Type
        /// </summary>
        /// <param name="roomType"></param>
        /// <returns></returns>
        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult Create(RoomType roomType)
        {
            if (ModelState.IsValid)
            {
                roomType.UserCreated = HttpContext.User.Identity.Name;
                roomType.CreatedDate = DateTime.Now.ToSqlDateTime();
                roomType.IsActive = Convert.ToBoolean(Request["IsActive"]);
                _roomTypeRepository.Add(roomType);
                _roomTypeRepository.Save();
                return RedirectToAction("Details", new {id = roomType.RoomTypeId});
            }

            return View(new RoomTypeViewModel(roomType));
        }

        /// <summary>
        /// Edit Room Type
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Authorize]
        public ActionResult Edit(int id)
        {
            var roomType = _roomTypeRepository.GetRoomTypeById(id);
            return View(new RoomTypeViewModel(roomType));
        }

        /// <summary>
        /// Edit Room Type
        /// </summary>
        /// <param name="id"></param>
        /// <param name="collection"></param>
        /// <returns></returns>
        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            var roomType = _roomTypeRepository.GetRoomTypeById(id);
            if (ModelState.IsValid)
            {
                try
                {
                    roomType.UserModified = HttpContext.User.Identity.Name;
                    roomType.ModifiedDate = DateTime.Now;
                    UpdateModel(roomType, "RoomType");
                    _roomTypeRepository.Save();
                    return RedirectToAction("Details", new {id = roomType.RoomTypeId});
                }
                catch
                {
                    return View(new RoomTypeViewModel(roomType));
                }
            }
            return View(new RoomTypeViewModel(roomType));
        }

        /// <summary>
        /// Delete Room Type
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Authorize]
        public ActionResult Delete(int id)
        {
            var roomType = _roomTypeRepository.GetRoomTypeById(id);
            return roomType == null ? View("NotFound") : View(new RoomTypeViewModel(roomType));
        }

        /// <summary>
        /// Delete Room Type
        /// </summary>
        /// <param name="id"></param>
        /// <param name="confirmButton"></param>
        /// <returns></returns>
        [HttpPost, Authorize]
        public ActionResult Delete(int id, string confirmButton)
        {
            var roomType = _roomTypeRepository.GetRoomTypeById(id);

            if (roomType != null)
            {
                _roomTypeRepository.Delete(roomType);
                _roomTypeRepository.Save();
                return View("Deleted");
            }
            return View("NotFound");
        }

        /// <summary>
        /// Handle Unknown Action
        /// </summary>
        /// <param name="actionName"></param>
        /// <exception cref="HttpException"></exception>
        protected override void HandleUnknownAction(string actionName)
        {
            throw new HttpException(404, "Action not found");
        }

        #endregion
    }
}